﻿using System;
using ASPItalia.ModelVirtualCasting.Messaging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net.Mail;

namespace ASPItalia.ModelVirtualCasting.Test
{
    [TestClass()]
    public class MessengerTest
    {
        private TestContext _testContextInstance;

        public TestContext TestContext
        {
            get { return _testContextInstance; }
            set { _testContextInstance = value; }
        }

        [ClassInitialize()]
        public static void Initialize(TestContext testContext)
        {
        }

        [ClassCleanup()]
        public static void Cleanup()
        {
        }
        
        [TestInitialize()]
        public void TestInitialize()
        {
        }
        
        [TestCleanup()]
        public void TestCleanup()
        {
        }

        [TestMethod()]
        public void MessengerConstructorTest1()
        {
            string to = "test@test.com";
            string subject = "TEST";
            string body = "This is a test!";

            Messenger target = new Messenger(to, subject, body);

            Assert.AreEqual(Configuration.EmailMessageSender, target.Message.From);
            Assert.AreEqual("test@test.com", target.Message.To);
            Assert.AreEqual("TEST", target.Message.Subject);
            Assert.AreEqual("This is a test!", target.Message.Body);
        }

        /// <summary>
        ///A test for Messenger Constructor
        ///</summary>
        [TestMethod()]
        public void MessengerConstructorTest2()
        {
            string from = "noreply@test.com";
            string to = "test@test.com";
            string subject = "TEST";
            string body = "This is a test!";

            Messenger target = new Messenger(from, to, subject, body);

            Assert.AreEqual("noreply@test.com", target.Message.From);
            Assert.AreEqual("test@test.com", target.Message.To);
            Assert.AreEqual("TEST", target.Message.Subject);
            Assert.AreEqual("This is a test!", target.Message.Body);
        }

        [TestMethod()]
        public void MessengerConstructorTest3()
        {
            string from = "noreply@test.com";
            string to = "test@test.com";
            string subject = "TEST";
            string body = "This is a test!";

            EmailMessage message = new EmailMessage { From = from, To = to, Subject = subject, Body = body };
            Messenger target = new Messenger(message);

            Assert.AreEqual("noreply@test.com", target.Message.From);
            Assert.AreEqual("test@test.com", target.Message.To);
            Assert.AreEqual("TEST", target.Message.Subject);
            Assert.AreEqual("This is a test!", target.Message.Body);
        }

        [TestMethod()]
        [ExpectedException(typeof(SmtpException))]
        public void SendEmailMessageTest()
        {
            string from = "noreply@test.com";
            string to = "test@test.com";
            string subject = "TEST";
            string body = "This is a test!";
            
            Messenger target = new Messenger(from, to, subject, body);
            target.SendEmailMessage(true);
        }

        [TestMethod()]
        public void ToStringTest()
        {
            string from = "noreply@test.com";
            string to = "test@test.com";
            string subject = "TEST";
            string body = "This is a test!";

            Messenger target = new Messenger(from, to, subject, body);
            string actual = target.ToString();
            Assert.AreEqual("TEST", actual);

            target.Message = null;
            actual = target.ToString();
            Assert.AreEqual(string.Empty, actual);
        }

        [TestMethod()]
        public void MessageTest()
        {
            string from = "noreply@test.com";
            string to = "test@test.com";
            string subject = "TEST";
            string body = "This is a test!";

            Messenger target = new Messenger(new EmailMessage());
            EmailMessage expected = new EmailMessage { From = from, To = to, Subject = subject, Body = body };
            EmailMessage actual = null;

            target.Message = expected;
            actual = target.Message;

            Assert.AreEqual(expected.From, actual.From);
            Assert.AreEqual(expected.To, actual.To);
            Assert.AreEqual(expected.Subject, actual.Subject);
            Assert.AreEqual(expected.Body, actual.Body);
        }
    }
}
